package malagaAcoge.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.faces.context.FacesContext;

public class Utils {

	public String redirect(String url) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (facesContext.getExternalContext().getSessionMap().get("tecnico") == null) {
			return "login?faces-redirect=true";
		} else {
			return url + "?faces-redirect=true";
		}
	}

	public String encrypt(String pass) {
		try{String MD5 = "MD5";
		byte[] buffer = pass.getBytes();
		MessageDigest messageDigest = MessageDigest.getInstance(MD5);
		messageDigest.reset();
		messageDigest.update(buffer);
			return toHexadecimal(messageDigest.digest());
		} catch (NoSuchAlgorithmException ex) {
            System.err.println("Error creando Digest");
		}
		return null;
		

	}
	private static String toHexadecimal(byte[] digest){
        String hash = "";
        for(byte aux : digest) {
            int b = aux & 0xff;
            if (Integer.toHexString(b).length() == 1) hash += "0";
            hash += Integer.toHexString(b);
        }
        return hash;
    }

}
